{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div class=\"contentcontainer med left\" style=\"margin-left: -50px;\">\n",
    "<dl class=\"dl-horizontal\">\n",
    "  <dt>Title</dt> <dd>NdOverlay Container</dd>\n",
    "  <dt>Dependencies</dt> <dd>Bokeh</dd>\n",
    "  <dt>Backends</dt> <dd><a href='./NdOverlay.ipynb'>Bokeh</a></dd> <dd><a href='../matplotlib/NdOverlay.ipynb'>Matplotlib</a></dd> <dd><a href='../plotly/NdOverlay.ipynb'>Plotly</a></dd>\n",
    "</dl>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import holoviews as hv\n",
    "hv.extension('bokeh')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "An ``NdOverlay`` is a multi-dimensional dictionary of HoloViews elements presented overlayed in the same space. An ``NdOverlay`` can be considered as a special-case of ``HoloMap`` that can only hold a single type of element at a time. Unlike a regular ``Overlay`` that can be built with the ``*`` operator, the items in an ``NdOverlay`` container have corresponding keys and must all have the same type. See the [Building Composite Objects](../../../user_guide/06-Building_Composite_Objects.ipynb) user guide for details on how to compose containers."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ``NdOverlay`` holds dictionaries"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using the ``sine_curve`` function below, we can declare a dictionary of ``Curve`` elements, where the keys correspond to the frequency values:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "frequencies = [0.5, 0.75, 1.0, 1.25]\n",
    "\n",
    "def sine_curve(phase, freq):\n",
    "    xvals = [0.1* i for i in range(100)]\n",
    "    return hv.Curve((xvals, [np.sin(phase+freq*x) for x in xvals]))\n",
    "\n",
    "curve_dict = {f:sine_curve(0,f) for f in frequencies}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We now have a dictionary where the frequency is the key and the corresponding curve element is the value. We can now turn this dictionary into an ``NdOverlay`` by declaring the keys as corresponding to the frequency key dimension:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ndoverlay = hv.NdOverlay(curve_dict, kdims='frequency')\n",
    "ndoverlay"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that the ``NdOverlay`` is displayed with a legend using colors defined by the ``Curve`` color cycle. For more information on using ``Cycle`` to define color cycling, see the [User Guide]."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ``NdOverlay`` is multi-dimensional"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By using tuple keys and making sure each position in the tuple is assigned a corresponding ``kdim``, ``NdOverlays`` allow visualization of a multi-dimensional space:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "curve_dict_2D = {(p,f):sine_curve(p,f) for p in [0, np.pi/2] for f in [0.5, 0.75]}\n",
    "ndoverlay = hv.NdOverlay(curve_dict_2D, kdims=['phase', 'frequency'])\n",
    "ndoverlay"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ``NdOverlay`` is similar to ``HoloMap``"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Other than the difference in the visual semantics, whereby ``NdOverlay`` displays its contents overlaid, ``NdOverlay`` are very similar to ``HoloMap`` (see the [``HoloMap``](./HoloMap.ipynb) notebook for more information).\n",
    "\n",
    "One way to demonstrate the similarity of these two containers is to cast our ``ndoverlay`` object to ``HoloMap`` and back to an ``NdOverlay``:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hmap = hv.HoloMap(ndoverlay)\n",
    "hmap + hv.NdOverlay(hmap)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
